<template>
    <div>
      <!--面包屑-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片-->
      <el-card>
        <!--收缩区域-->
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList()">
             <el-button slot="append" icon="el-icon-search" @click="getUserList()"></el-button>
            </el-input>
          </el-col>
          <el-col :span="6">
            <el-button type="primary">添加用户</el-button>
          </el-col>
        </el-row>

        <!--用户列表区域-->
        <el-table :data="userlist" stripe border>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="username" label="姓名" ></el-table-column>
          <el-table-column prop="email" label="邮箱" ></el-table-column>
          <el-table-column prop="mobile" label="电话" ></el-table-column>
          <el-table-column prop="role_name" label="角色" ></el-table-column>
          <el-table-column prop="mg_state" label="状态" >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.mg_state" @change="updateUserState(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
                <el-button type="primary" icon="el-icon-edit" size="mini" ></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="修改角色" placement="top" :enterable="false">
                <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

      </el-card>


    </div>

</template>

<script>
  export default {
    data() {
      return {
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 2
        },
        userlist: [],
        total: 0
      }
    },created () {
        this.getUserList()
    },
    methods: {
        getUserList(){
          this.$http.get('/users',{params: this.queryInfo}).then(res => {
            if(res.status !== 200) return this.$message.error('获取用户列表失败')
            this.userlist = res.data.data.users
            this.total = res.data.data.total
          })
        },
        handleSizeChange(newSize){
          this.queryInfo.pagesize = newSize
          this.getUserList()
        },
        handleCurrentChange(newPage){
          this.queryInfo.pagenum = newPage
          this.getUserList()
        },
        updateUserState(row){
          this.$http.put(`users/${row.id}/state/${row.mg_state}`).then(res =>{
            if(res.status !== 200){
              row.mg_state = !row.mg_state
              return this.$message.error('修改用户状态失败')
            }
            this.$message.success('修改状态成功')
          })
        }
    }
  }
</script>

<style scoped lang="less">

</style>
